package demo.config;

import demo.swagger.ApiModelEnumPropertyBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.sql.Timestamp;
import java.util.Arrays;

/**
 * @author meilin.huang
 * @date 2022-02-16 4:16 PM
 */
@EnableSwagger2
@Configuration(proxyBeanMethods = false)
public class SwaggerConfig {

    /**
     * 注册枚举值属性建造器，用于增强swagger对枚举显示描述的支持
     */
    @Bean
    public ApiModelEnumPropertyBuilder apiModelEnumPropertyBuilder() {
        return new ApiModelEnumPropertyBuilder();
    }

    @Bean
    public Docket createRestApi() {
        Parameter tokenHeaderParam = new ParameterBuilder()
                .name("token")
                .description("token")
                .modelRef(new ModelRef("string"))
                .parameterType("header").build();

        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .globalOperationParameters(Arrays.asList(tokenHeaderParam))
                .select()
                .apis(RequestHandlerSelectors.basePackage("demo")).paths(PathSelectors.any()).build()
                .directModelSubstitute(Timestamp.class, Long.class);
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("demo接口")
                .description("demo接口")
                .version("1.0")
                .build();
    }
}
